package com.yupi.project.controller;


import com.yupi.project.annotation.AuthCheck;
import com.yupi.project.mapper.InterfaceInfoMapper;
import com.yupi.project.mapper.InterfaceInvokeInfoMapper;
import com.yupi.project.mapper.UserInterfaceInfoMapper;
import com.yupi.project.service.InterfaceInfoService;
import com.yupi.yuapicommon.model.common.BaseResponse;
import com.yupi.yuapicommon.model.common.ResultUtils;
import com.yupi.yuapicommon.model.entity.InterfaceInfo;
import com.yupi.yuapicommon.model.vo.InterfaceInvokeInfoVo;
import com.yupi.yuapicommon.model.vo.UserInterfaceInfoVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AnalysisController
 * @Description 分析接口调用情况（待启用）
 * @Author zzsource
 * @Date 2023/10/17 14:59
 * @Version 1.0
 **/
@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {

    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;
    @Resource
    private InterfaceInfoService interfaceInfoService;
    @Resource
    private InterfaceInvokeInfoMapper interfaceInvokeInfoMapper;
    @Resource
    private UserInterfaceInfoMapper userInterfaceInfoMapper;

    @GetMapping("/top/interface/invoke")
    @AuthCheck(mustRole = "admin")
    @ApiOperation("获取调用热度前3接口信息(admin)")
    public BaseResponse<List<UserInterfaceInfoVO>> listTopInvokeInterfaceInfo() {
        List<UserInterfaceInfoVO> list = new ArrayList<>();
        // 找到调用次数最多的3个接口
        List<InterfaceInvokeInfoVo> userInterfaceInfoList = interfaceInvokeInfoMapper.listTopInvokeInterfaceInfo(3);
        userInterfaceInfoList.forEach(userInterfaceInfo ->{
            Integer id = userInterfaceInfo.getInterfaceInfoId();
            InterfaceInfo interfaceInfo = interfaceInfoService.getById(id);
            UserInterfaceInfoVO userInterfaceInfoVO = new UserInterfaceInfoVO();
            BeanUtils.copyProperties(interfaceInfo, userInterfaceInfoVO);
            userInterfaceInfoVO.setTotalNum(userInterfaceInfo.getTotalNum());
            list.add(userInterfaceInfoVO);
        });
        return ResultUtils.success(list);
        //List<UserInterfaceInfo> userInterfaceInfoList = userInterfaceInfoMapper.listTopInvokeInterfaceInfo(3);
        //Map<Long, List<UserInterfaceInfo>> interfaceInfoIdObjMap =
        //        userInterfaceInfoList.stream().collect(Collectors.groupingBy(UserInterfaceInfo::getInterfaceInfoId));
        //QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        //queryWrapper.in("id", interfaceInfoIdObjMap.keySet());
        //List<InterfaceInfo> interfaceInfoList = interfaceInfoMapper.selectList(queryWrapper);
        //List<UserInterfaceInfoVO> userInterfaceInfoVOList = interfaceInfoList.stream().map(interfaceInfo -> {
        //    UserInterfaceInfoVO userInterfaceInfoVO = new UserInterfaceInfoVO();
        //    BeanUtils.copyProperties(interfaceInfo, userInterfaceInfoVO);
        //    int totalNum = interfaceInfoIdObjMap.get(interfaceInfo.getId()).get(0).getTotalNum();
        //    userInterfaceInfoVO.setTotalNum(totalNum);
        //    return userInterfaceInfoVO;
        //}).collect(Collectors.toList());
        //// 如果查询的Top 数据为空，抛出异常
        //if(CollectionUtil.isEmpty(userInterfaceInfoVOList)){
        //    throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        //}
        //return ResultUtils.success(userInterfaceInfoVOList);
    }
}
